package com.news.servlet;

import com.news.db.OpDB;
import com.news.model.Person;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        PrintWriter out = resp.getWriter();
        OpDB opdb = new OpDB();
        if (method != null && method.equals("person")) {
            int id = Integer.parseInt(req.getParameter("strid"));
            Person p = opdb.OpGetPerson(id);
            req.getSession().setAttribute("person", p);
            RequestDispatcher dispatcher = req.getRequestDispatcher("view/IndexTemp.jsp?mainPage=person");
            dispatcher.forward(req, resp);
        } else if (method.equals("vote")) {
            String person_name = req.getParameter("person_name");
            if (person_name == null){
                out.println("<script>alert('请选择投票人物!'); window.location.href='view/IndexTemp.jsp';</script>");
                return;
            }
            int id = Integer.parseInt(req.getParameter("person_name"));
            String isVote = (String) req.getServletContext().getAttribute("isVote");
            if (isVote == null) {
                if (opdb.OpVotePerson(id)) {
                    Person p = opdb.OpGetPerson(id);
                    req.getSession().setAttribute("person", p);
                    req.getServletContext().setAttribute("isVote", "ok");
                    out.println("<script>alert('投票成功'); window.location.href='view/IndexTemp.jsp?mainPage=person';</script>");
                }
            } else if (isVote.equals("ok")) {
                out.println("<script>alert('您已投票'); window.location.href='view/IndexTemp.jsp?mainPage=person';</script>");
            }
        } else if(method.equals("link")){
            int id=Integer.parseInt(req.getParameter("id"));
            switch(id){
                case 1:
                    resp.sendRedirect("https://www.qq.com");
                    break;
                case 2:
                    resp.sendRedirect("http://news.baidu.com");
                    break;
                case 3:
                    resp.sendRedirect("http://www.ifeng.com");
                    break;
                case 4:
                    resp.sendRedirect("https://www.sina.com");
                    break;
            }

        }
    }
}
